﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Documents;
using System.Text.RegularExpressions;
using System.Windows.Media;
using System.Diagnostics;
using System.Windows.Input;
using myBoard.Framework;

namespace myTwitter.Converters
{ 
    public class TextToFlowDocumentConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            FlowDocument fd = new FlowDocument();
            Paragraph para = new Paragraph();
            string text = value.ToString();
            
            //Remove HTML tags
            string HTML_TAG_PATTERN = "<.*?>";
            text = Regex.Replace(text, HTML_TAG_PATTERN, string.Empty);

            string[] words = text.ToString().Split(' ');
            foreach (string word in words)
            {
                if (IsHyperlink(word))
                {
                    Run r = new Run(word);
                    Hyperlink h = new Hyperlink(r);
                    try
                    {
                        h.NavigateUri = new Uri(word);
                    }
                    catch { }
                    h.Cursor = Cursors.Hand;
                    h.MouseEnter += new MouseEventHandler(delegate(object sender, MouseEventArgs e)
                        {
                            Mouse.OverrideCursor = Cursors.Hand;
                        });
                    h.MouseLeave += new MouseEventHandler(delegate(object sender, MouseEventArgs e)
                        {
                            Mouse.OverrideCursor = null;
                        });
                    h.MouseDown += new System.Windows.Input.MouseButtonEventHandler(delegate(object sender, System.Windows.Input.MouseButtonEventArgs e)
                        {
                            try
                            {
                                Hyperlink link = sender as Hyperlink;
                                GlobalController.HideMyBoard();
                                Process.Start(link.NavigateUri.AbsoluteUri);
                                Connector.HideMyBoard();
                            }
                            catch (Exception ex)
                            {
                                ExceptionsManager.LogException("myTwitter", ex, false);
                            }
                        });
                    h.Foreground = new SolidColorBrush(Color.FromArgb(255, 115, 185, 255));
                    para.Inlines.Add(h);                        
                }
                else
                {
                    para.Inlines.Add(word);
                }
                para.Inlines.Add(" ");
            }

            fd.Blocks.Add(para);
            return fd;
        }

        private bool IsHyperlink(string word)
        {
            Regex regex = new Regex(@"(https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*");
            Match match = regex.Match(word);
            if (match.Length > 0)
                return true;
            else
                return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
